#include "lcd.h"

extern void LCD_Initial()
{
  pinMode(LCD_RS,OUTPUT);
  pinMode(LCD_SCLK,OUTPUT);
  pinMode(LCD_SID,OUTPUT);
  pinMode(LCD_RESET,OUTPUT);
  pinMode(LCD_S1,INPUT_PULLUP);
  pinMode(LCD_S2,INPUT_PULLUP);
  pinMode(LCD_S3,INPUT_PULLUP);
  digitalWrite(LCD_RESET,LOW);
  delay(50);
  digitalWrite(LCD_RESET,HIGH);
  delay(50);
  LCD_Transfer_command(0xe2);
  LCD_Transfer_command(0x2c);
  LCD_Transfer_command(0x2e);
  LCD_Transfer_command(0x2f);
  LCD_Transfer_command(0x23);
  LCD_Transfer_command(0x81);
  LCD_Transfer_command(0x28);
  LCD_Transfer_command(0xa2);
  LCD_Transfer_command(0xc8);
  LCD_Transfer_command(0xa0);
  LCD_Transfer_command(0xaf);
}

extern void LCD_Enable(boolean en) {
  pinMode(LCD_CS,OUTPUT);
  if (en) {
    digitalWrite(LCD_CS,HIGH);
  } else {
    digitalWrite(LCD_CS,LOW);
  }
}

#define LCD_MAX_RAW                 4       // 液晶最大行数
#define LCD_MAX_COL                 120     // 液晶最大列数
extern unsigned char LCD_Draw_Char(char c, unsigned char row, unsigned char col) {

  unsigned char * add;
  if (c < 128) add = asc + (c - 0x20) * 16;
  else add = zh_cn + (c - 0x80) * 32;
  unsigned int i,j; //计数器
  unsigned char col_0 = col;                    // 低四位
  unsigned char col_1 = col >> 4;           // 高四位

  //----------------输入检测-------------
  //--------------返回0表示输入出错---------------
  if (col > 127) {
    return 0;
  }
  if (row > 3) {
    return 0;
  }

  for (i = 0; i < 2; i++)
  {
    LCD_Transfer_command(0xb0 + row * 2 + i);            //设置“页”
    LCD_Transfer_command(0x10 + (col_1 & 0x0f));            //设置“列”以及列的高四位
    LCD_Transfer_command(0x00 + (col_0 & 0x0f));            //设置“列”以及列的低四位
    for (j = 0; j < 8; j++)
    {
      LCD_Transfer_data(*add);
      add++;
    }
  }

  delay(1);

  if (c >= 128) {
    col = col + 8;
    if (col > 127) return 0;
    col_0 = col;
    col_1 = col >> 4;
    for (i = 0; i < 2; i++)
    {
      LCD_Transfer_command(0xb0 + row * 2 + i);            //设置“页”
      LCD_Transfer_command(0x10 + (col_1 & 0x0f));            //设置“列”以及列的高四位
      LCD_Transfer_command(0x00 + (col_0 & 0x0f));            //设置“列”以及列的低四位
      for (j = 0; j < 8; j++)
      {
        LCD_Transfer_data(*add);
        add++;
      }
    }
  }

  return 1;                                 //返回1表示执行成功！
}

void LCD_print(String str, int pos_y, int pos_x) {
  int str_len = str.length();
  int _pos_x = pos_x;
  int _pos_y = pos_y;
  char ch;
  for (int i = 0; i < str_len; i++) {
    ch = str.charAt(i);
    LCD_Draw_Char(ch, _pos_y, _pos_x*8);
    if (ch > 127) ++_pos_x;
    if ((++_pos_x)*8 > 120) {
      _pos_x = 0;
      _pos_y++;
    }
  }
}

//===============clear all dot martrics=============
extern void LCD_Clear()
{
  unsigned char i,j;
  for(i=0;i<9;i++)
  {
    LCD_Transfer_command(0xb0+i);
    LCD_Transfer_command(0x10);
    LCD_Transfer_command(0x00);
    for(j=0;j<132;j++)
    {
      LCD_Transfer_data(0x00);
    }
  }
}
/*=======写指令========*/
extern void LCD_Transfer_command(byte data1) {
  char i;
  digitalWrite(LCD_CS,LOW);
  digitalWrite(LCD_RS,LOW);//LCD_rs=0;
  for(i=0;i<8;i++)
  {
    digitalWrite(LCD_SCLK,LOW);
    if(data1&0x80)
      digitalWrite(LCD_SID,HIGH);
    else digitalWrite(LCD_SID,LOW);
//    delayMicroseconds(2);
    digitalWrite(LCD_SCLK,HIGH);
//    delayMicroseconds(2);
    data1=data1<<=1;
  }
  digitalWrite(LCD_CS,HIGH);
}
/*--------写数据------------*/
extern void LCD_Transfer_data(byte data1) {
  char i;
  digitalWrite(LCD_CS,LOW);
  digitalWrite(LCD_RS,HIGH);
  for(i=0;i<8;i++)
  {
    digitalWrite(LCD_SCLK,LOW);
    if(data1&0x80) digitalWrite(LCD_SID,HIGH);
    else digitalWrite(LCD_SID,LOW);
    digitalWrite(LCD_SCLK,HIGH);//LCD_sclk=1;
    data1=data1<<=1;
  }
  digitalWrite(LCD_CS,HIGH);
}

